package com.example.rbac.controller;

import com.example.rbac.common.web.JsonData;
import com.example.rbac.dto.DeptLevelDto;
import com.example.rbac.param.DeptParam;
import com.example.rbac.service.SysDeptService;
import com.example.rbac.service.SysTreeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author XieLongzhen
 * @date 2018/12/28 18:43
 */
@Controller
@RequestMapping("/sys/dept")
@Slf4j
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private SysTreeService sysTreeService;

    @GetMapping("/dept.page")
    public ModelAndView page() {
        return new ModelAndView("dept");
    }

    @PostMapping("/save.json")
    @ResponseBody
    public JsonData saveDept(DeptParam deptParam) {
        if (sysDeptService.save(deptParam)) {
            return JsonData.success();
        }
        return JsonData.fail("操作失败！");
    }

    @GetMapping("/tree.json")
    @ResponseBody
    public JsonData tree() {
        List<DeptLevelDto> dtoList = sysTreeService.deptTree();
        return JsonData.success(dtoList);
    }

    @PostMapping("/update.json")
    @ResponseBody
    public JsonData updateDept(DeptParam deptParam) {
        sysDeptService.update(deptParam);
        return JsonData.success();
    }

    @GetMapping("/delete.json")
    @ResponseBody
    public JsonData delete(@RequestParam("id") int id) {
        sysDeptService.delete(id);
        return JsonData.success();
    }
}
